﻿using System;

namespace Two
{
    
    public delegate void TickHandler(object sender, TickEventArgs args);
    public delegate void AlarmHandler(object sender, AlarmEventArgs args);
    public class TickEventArgs
    {
        public DateTime time { get; set; }
    }
    public class AlarmEventArgs
    {
        public DateTime time { get; set; }
    }

    public class Clock
    {
        public event TickHandler OnTick;
        public event AlarmHandler OnAlarm;
        private DateTime alarmtime;
        public void setAlarmtime(DateTime time1)
        {
            alarmtime = time1;
        }

        public void Run(DateTime time,DateTime alarm)
        {
            setAlarmtime(alarm);
            //Console.WriteLine($"{Hour} {end}");
            while (time<alarm)
            {
                Tick(time);
                time = time.AddMinutes(1);
            }
            Alarm(alarm);

        }
        public void Tick(DateTime time)
        {
            TickEventArgs args = new TickEventArgs()
            {
                time = time
            };
            OnTick(this, args);
        }
        public void Alarm(DateTime time)
        {
            Console.WriteLine("clock alarming");
            AlarmEventArgs args = new AlarmEventArgs()
            {
                time = time
            };
            //触发onAlarm事件
            OnAlarm(this, args);
        }
    }

    public class Form
    {
        public Clock clock1 = new Clock();
        public Form()
        {
            //为btn的onTick事件添加两个处理方法
            clock1.OnTick += new TickHandler(Clk_OnTick);
            clock1.OnAlarm += Clk_OnAlarm;
        }
        void Clk_OnTick(object sender, TickEventArgs args)
        { Console.WriteLine($"Ticking at {args.time}........."); }

        void Clk_OnAlarm(object sender, AlarmEventArgs args)
        { Console.WriteLine("Alarming!"); }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Form form1 = new Form();
            form1.clock1.Run(DateTime.Now,DateTime.Now.AddHours(1));
        }
    }
}

        
